package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import util.DatabaseConnector;

public class MySpaceModel {

    private int postID;
    private String userSource;
    private String post;
    private String created;
    private int userTier;
    private String user;
    Connection con = null;
    private ArrayList<MySpaceModel> containerList;

    public MySpaceModel() throws SQLException {  
        //con = DatabaseConnector.getDBConnection();
       //con.setAutoCommit(false);
        containerList = new ArrayList<MySpaceModel>();
    }

    /**
     * @return the containerList
     */
    public ArrayList<MySpaceModel> getContainerList() {
        return containerList;
    }

    /**
     * @param containerList the containerList to set
     */
    public void setContainerList(ArrayList<MySpaceModel> containerList) {
        this.containerList = containerList;
    }

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    public int getPostID() {
        return postID;
    }

    public void setPostID(int postID) {
        this.postID = postID;
    }

    public int getUserTier() {
        return userTier;
    }

    public void setUserTier(int userTier) {
        this.userTier = userTier;
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUserLink() {
        return userSource;
    }

    public void setUserLink(String userLink) {
        this.userSource = userLink;
    }

    public void addPost() throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;       

        try {
            String InsertPostSQL = "INSERT INTO tbl_posts_mySpace (UserSource, User, Title, Post, Created) "
                + "VALUES (?, ?, ?, ?, ?)";
            con = DatabaseConnector.getDBConnection();
            for (MySpaceModel i : getContainerList()) {
                
                ps = con.prepareStatement(InsertPostSQL);
                String tempPost = i.getPost().trim();
                if (!tempPost.isEmpty()) {

                    String tempUserSource = i.getUserLink();
                    String[] extractUser = tempUserSource.split("/");
                    String tempUser;
                    int arraySize = extractUser.length;
                    
                    if (arraySize != 4) {
                        tempUser = i.getUser();
                    } else {
                        tempUser = extractUser[3];
                    }                  
                   
                    if(tempUser == null){
                        tempUser = "Guest";
                    }

                    String tempTitle = i.getTitle().trim();
                    String tempCreated = i.getCreated();

                    ps.setString(1, tempUserSource);
                    ps.setString(2, tempUser);
                    ps.setString(3, tempTitle);
                    ps.setString(4, tempPost);
                    ps.setString(5, tempCreated);
                    ps.executeUpdate();

                    rs = con.prepareStatement("SELECT LAST_INSERT_ID()").executeQuery();
                    if (rs.next()) {
                        setPostID(rs.getInt(1));
                    }
                    //con.commit();
                }
            }

        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException ex) {
            }
            System.out.println("Error while loading data...." + e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }
    }
}
